#pragma once
#include "ZLibIncludeHeader.h"



inline vecQByteArray g_SplitByteArray(const QByteArray& dataArr, int iPerSize)
{
	vecQByteArray vecArr;
	int iArrSize = dataArr.size();
	for (int iStartPos = 0; iStartPos<iArrSize; iStartPos+=iPerSize )
	{
		QByteArray subArr = dataArr.mid(iStartPos, iPerSize);
		if (subArr.size() > 0)
		{
			vecArr.push_back(subArr);
		}
	}

	return vecArr;
}

inline int QByteArrayToInt(QByteArray& byteArr) {
	int iVal = byteArr[0] & 0x000000FF;    
	iVal |= ((byteArr[1] << 8) & 0x0000FF00);    
	iVal |= ((byteArr[2] << 16) & 0x00FF0000);    
	iVal |= ((byteArr[3] << 24) & 0xFF000000);    
	return iVal;    
}   

template < class Type >
inline void ClearContainer( std::vector<Type> &vec )
{
	for (auto i=vec.begin(); i!=vec.end(); i++)
	{
		delete (*i);
	}
	vec.clear();
}

template < class Type >
inline int EraseFromContainer( std::vector<Type> &vec, const Type& val )
{
	int iEraseSize = 0;
	for (auto i=vec.begin(); i!=vec.end();)
	{
		if ((*i) == val)
		{
			i = vec.erase(i);
			iEraseSize++;
		}
		else
		{
			 i++;
		}
	}

	return iEraseSize;
}

template < class Type >
inline QDataStream& operator<<( QDataStream& qStream, const std::vector<Type> &vec )
{
	std::vector<Type>::size_type nSize = vec.size();
	qStream << nSize;
	for( std::vector<Type>::size_type i=0; i<nSize; i++ )
	{
		qStream << vec[i];
	}
	return qStream;
}

template < class Type >
inline QDataStream& operator>>( QDataStream& qStream, std::vector<Type> &vec )
{
	std::vector<Type>::size_type nSize = 0;
	qStream >> nSize;
	vec.resize( nSize );
	for( std::vector<Type>::size_type i=0; i<nSize; i++ )
	{
		qStream >> vec[i];
	}
	return qStream;
}

template < class Type >
inline QDataStream& operator<<( QDataStream& qStream, const std::set<Type> &set )
{
	std::set<Type>::size_type nSize = set.size();
	qStream << nSize;
	for( std::set<Type>::const_iterator i=set.begin(); i!=set.end(); i++ )
	{
		qStream << (*i);
	}
	return qStream;
}

template < class Type >
inline QDataStream& operator>>( QDataStream& qStream, std::set<Type> &set )
{
	std::set<Type>::size_type nSize = 0;
	qStream >> nSize;

	Type val;
	for (std::set<Type>::size_type i=0; i<nSize; i++)
	{
		qStream >> val;
		set.insert(val);
	}
	
	return qStream;
}